package org.gitman.agentplatform.ui.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;

import org.gitman.agentplatform.core.exceptions.NoGraphException;
import org.gitman.agentplatform.ui.api.View;
import org.gitman.agentplatform.ui.api.View.MessageType;
import org.gitman.agentplatform.ui.simulation.SimulationPanel;

public class LoadSimulationAction extends UIAction{

	public LoadSimulationAction() {
		super("Load Simulation");
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser fc = new JFileChooser(System.getProperty("installDir") + "/simulations");
		int rc = fc.showOpenDialog(mainFrame);
		if(rc == JFileChooser.APPROVE_OPTION){
			File f = fc.getSelectedFile();
			try {
				mainFrame.loadSimulation(f);
				SimulationPanel simulationPanel = mainFrame.getSimulationPanel();
				simulationPanel.fireTableChanged();
				simulationPanel.loadDefaultAgentParams();
				platformManager.getView().init(platformManager.getSimulation().getAgentManager().getAllAgentsInfo());
			} catch (IOException ex) {
				View.showMessage(ex.getMessage(), MessageType.ERROR);
				ex.printStackTrace();
			} catch (NoGraphException ex2) {
				View.showMessage(ex2.getMessage(), MessageType.ERROR);
				ex2.printStackTrace();
			}
			
		}
	}

}
